package com.example.studykotlin.kotlin_actual_combat.art.animation

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.util.Log
import android.view.animation.AlphaAnimation
import android.view.animation.AnimationUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.studykotlin.Path
import com.example.studykotlin.R
import com.example.studykotlin.databinding.ActivityAnimtionBinding
import com.example.studykotlin.kotlin_actual_combat.BaseActivity.BaseActivity

@Route(path = Path.animation)
class AnimationActivity : BaseActivity() {
    private  val TAG = "AnimationActivity"
    private lateinit var binding: ActivityAnimtionBinding
    private var animationDrawableFlag = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAnimtionBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()
    }


    private fun initView() {
        val animation = AnimationUtils.loadAnimation(this,R.anim.animation_test)

        val alphaAnimation = AlphaAnimation(0f,1f)
        alphaAnimation.duration = 1000
        alphaAnimation.fillAfter = true

        binding.btnTestAnim.setOnClickListener {
            binding.btnTestAnim.startAnimation(alphaAnimation)
        }

        binding.btnFrame.setBackgroundResource(R.drawable.form_animation)
        val animationDrawable = binding.btnFrame.background as AnimationDrawable

        binding.btnFrame.setOnClickListener {
            animationDrawableFlag = if (!animationDrawableFlag) {
                animationDrawable.start()
                true
            }  else {
                animationDrawable.stop()
                false
            }
        }

        val objectAnimator = ObjectAnimator.ofFloat(binding.btnTestAttrs,"translationY",-100f,-90f,-80f,-70f,-60f,0f)

        val colorAnimator = ObjectAnimator.ofInt(binding.btnTestAttrs,"backgroundColor", Color.RED,Color.BLACK)

        val animatorSetStart = AnimatorSet()
        animatorSetStart.playTogether(
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"rotationX",0f,360f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"rotationY",0f,180f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"rotation",0f,-90f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"translationX",0f,90f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"translationY",0f,90f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"scaleX",1f,1.5f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"scaleY",1f,0.5f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"alpha",1f,0.25f,1f),
        )
        val animatorSetEnd = AnimatorSet()
        animatorSetEnd.playTogether(
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"rotationX",360f,0f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"rotationY",180f,0f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"rotation",-90f,0f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"translationX",90f,0f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"translationY",90f,0f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"scaleX",1.5f,1f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"scaleY",0.5f,1f),
            ObjectAnimator.ofFloat(binding.btnTestAttrs,"alpha",1f,0.25f,1f),
        )
        animatorSetStart.addListener(object : Animator.AnimatorListener{
            override fun onAnimationStart(animation: Animator?) {
                Log.e(TAG, "onAnimationStart: " )
            }

            override fun onAnimationEnd(animation: Animator?) {
                animatorSetEnd.duration = 5000
                animatorSetEnd.start()
            }

            override fun onAnimationCancel(animation: Animator?) {

            }

            override fun onAnimationRepeat(animation: Animator?) {

            }

        })

        binding.btnTestAttrs.setOnClickListener {
//            objectAnimator.start()
//            colorAnimator.start()
            animatorSetStart.duration = 10000
            animatorSetStart.start()
        }
    }
}